function findBreakPoint (text, width, context) {
    var min = 0;
    var max = text.length - 1;
     
    while (min <= max) {
        var middle = Math.floor((min + max) / 2);
        var middleWidth = context.measureText(text.substr(0, middle)).width;
        var oneCharWiderThanMiddleWidth = context.measureText(text.substr(0, middle + 1)).width;
        if (middleWidth <= width && oneCharWiderThanMiddleWidth > width) {
            return middle;
        }
        if (middleWidth < width) {
            min = middle + 1;
        } else {
            max = middle - 1;
        }
    }
     
    return -1;
}
function breakLinesForCanvas (TextObj,text,context) {
	
	var width=TextObj.width;
	var font=TextObj.weight+' '+TextObj.size+' '+TextObj.family;
   /* var canvas = document.getElementById('textcanvas');
    var context = canvas.getContext('2d');*/
    var result = [];
    var breakPoint = 0;
     
    if (font) {
        context.font = font;
    }
     context.font = font;
     context.fillStyle = TextObj.color;
     context.textAlign = TextObj.center;
     
     
    while ((breakPoint = findBreakPoint(text, width, context)) !== -1) {
        result.push(text.substr(0, breakPoint));
        text = text.substr(breakPoint);
    }
     
    if (text) {
        result.push(text);
    }
     
    return result;
}

function drawlinetext(TextObj,fn){	
	var canvas=document.createElement('canvas');		
	canvas.width=TextObj.width;
	canvas.height=TextObj.height;
	
	var context = canvas.getContext('2d');
	var tempresult=new Array;
	var strenter=''
	if(TextObj.format==""){//lym 2017 11.22 新增月历修改
	if(TextObj.text.indexOf("\n")>-1){
		tempresult=TextObj.text.split('\n');
		strenter="\n";
	}else if(TextObj.text.indexOf("\r")>-1){
		tempresult=TextObj.text.split('\r');
		strenter="\r";
	}else if(TextObj.text.indexOf("\r\n")>-1){
		tempresult=TextObj.text.split('\r\n');
		strenter="\r\n";
	}else if(TextObj.text.indexOf("\r\n")>-1){
		tempresult=TextObj.text.split('↵');
		strenter="↵";
	}else{
		tempresult.push(TextObj.text);
		strenter="\n";
	}
	}else{//lym 2017 11.22 新增月历修改
		tempresult.push(TextObj.text);
		
	}
	var result=new Array;
	if(TextObj.ischange){	
		if(tempresult.length>0){
			for(var i=0; i<tempresult.length; i++){
				var temp_res=breakLinesForCanvas(TextObj,tempresult[i].toString(),context);
				 result=result.concat(temp_res); 
			}
		}else{
			result=breakLinesForCanvas(TextObj,TextObj.text,context);
		}
	}else{
		result=tempresult;		
		 var font=TextObj.weight+' '+TextObj.size+' '+TextObj.family;	  
	     context.font = font;
	     context.fillStyle = TextObj.color;
	     context.textAlign = TextObj.center;
	}
	
		
	var context_x=0;
	if(TextObj.center=="center"){
		context_x=TextObj.width/2;
	}else if(TextObj.center=="right"){
		context_x=TextObj.width;
	}else{
	context_x=0;	
	}
	
	
	result.forEach(function (line, index) {
		context.textBaseline="top";
		context.fillText(line, context_x, TextObj.lineHeight * index);
		//context.fillText(line, context_x, TextObj.lineHeight * (index+1));	
		//context.fillText(line, context_x, parseInt(TextObj.lineHeight * (index+1)+Number(TextObj.size.split("p")[0])/2));	
	});
	/*console.log(canvas.toDataURL());
	console.log(result.join('↵'));*/
	
	fn(result,strenter,canvas);
}


/*//文字旋转
function textRoate(data,width,height,ctx2,transform,rx,ry,px,py,_x,_y) {
	
	var angle = transform * Math.PI / 180; // angle of rotation in radians
	//var rx = 300, ry = 200; // the rotation x and y
	//var px = 300, py = 50; // the objects center x and y
	var radius = ry - py; // the difference in y positions or the radius
	var dx = rx + radius * Math.sin(angle); // the draw x
	var dy = ry - radius * Math.cos(angle); // the draw y	
	ctx2.translate(dx, dy);
	ctx2.rotate(angle);
	ctx2.translate(-dx, -dy);
	//ctx2.drawImage(data, dx - width / 2, dy - height / 2);
	ctx2.drawImage(data, dx - width / 2, dy - height / 2);
	console.log(dx,dy)
	console.log(_x,_y)
	console.log(dx - width / 2,dy - height / 2)
}*/


//文字旋转
function textRoate(data,width,height,ctx2,transform,_x,_y,scale) {
	var Roatecanvas=document.createElement('canvas');
	var Roatecontext= Roatecanvas.getContext('2d');
		transform = transform % 360;	
		var w = width;
		var h = height;	
		var x = w / -2;
		var y = h / -2;
	
		if(transform % 180 != 0) {
			Roatecanvas.width = h;
			Roatecanvas.height = w;			
		} else {
			Roatecanvas.width = w;
			Roatecanvas.height = h;			
		}
	 	Roatecontext.clearRect(0, 0, Roatecanvas.width, Roatecanvas.height);
		Roatecontext.translate(Roatecanvas.width / 2, Roatecanvas.height / 2);
		Roatecontext.rotate(transform * Math.PI / 180);
		Roatecontext.drawImage(data, x, y);
	
	/*console.log(Roatecanvas.toDataURL());*/
	ctx2.drawImage(Roatecanvas,_x,_y,Roatecanvas.width/scale,Roatecanvas.height/scale);
	ctx2.save();
	ctx2.clip();	
	ctx2.restore();
}



